//
//  NSString+TYCodec.m
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import "NSString+TYCodec.h"

#import <Availability.h>
#import "NSData+TYCodec.h"

@implementation NSString (TYCodec)

+ (NSString *)ty_stringWithBase64EncodedString:(NSString *)string {
    NSData *data = [NSData ty_dataWithBase64EncodedString:string];
    if (data) {
        return [[self alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return nil;
}

- (NSString *)ty_base64EncodedString {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    return [data ty_base64EncodedString];
}

- (NSString *)ty_base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    return [data ty_base64EncodedStringWithWrapWidth:wrapWidth];
}

- (NSString *)ty_base64DecodedString {
    return [NSString ty_stringWithBase64EncodedString:self];
}

- (NSData *)ty_base64DecodedData {
    return [NSData ty_dataWithBase64EncodedString:self];
}

@end
